package com.yongjie.springbootlogin.controller;

import com.yongjie.springbootlogin.entity.DoctorInfo;
import com.yongjie.springbootlogin.service.DoctorInfoServiceImpl;
import com.yongjie.springbootlogin.service.LoginService;
import com.yongjie.springbootlogin.utils.ResultMessage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 医生信息
 * @author 13468
 */
@Controller
public class DoctorInfoController {
    @Autowired
    DoctorInfoServiceImpl doctorInfoService;
    @Autowired
    private LoginService userLoginService;
    @Autowired
    LoginService loginService;
    /**
     * 获取医生信息
     * @param map
     * @return
     */
    @RequestMapping(value = "/DoctorInfo")
    public String performancepage(Map<String ,Object>map){
        List<DoctorInfo> doctorInfos =doctorInfoService.selectAll();
        map.put("doctorInfos",doctorInfos);
        return "DoctorInfo/DoctorInfo";
    }

    @PostMapping(value = "/checkSelct")
    public String checkSelct(Map<String ,Object> map ,
                             @RequestParam(value = "docInof",required = false) String dname,
                             @RequestParam(value = "docInof",required = false)String phone){
        if(dname.equals("")){
            dname=null;
        }
        if(phone.equals("")){
            phone=null;
        }
        List<DoctorInfo> doctorInfos = doctorInfoService.checkSelct(dname,  phone);
        map.put("doctorInfos",doctorInfos);
        return "DoctorInfo/DoctorInfo";
    }

    /**
     * 查看单个详情信息
     * @return
     */
    @RequestMapping("/lookInfo")
    public String lookInfo(String id,Map<String,Object> map){
        DoctorInfo aDocInfo = doctorInfoService.lookInfo(id);
        map.put("aDocInfo",aDocInfo);
        return "DoctorInfo/DocInfoShow";
    }

    /**
     * 添加医生页面
     * @return
     */
    @RequestMapping("/addDoctorpage")
    public String addDoctorpage() {
        return "DoctorInfo/addDoctor";
    }
    /**
     * 添加医生数据
     */
    @RequestMapping("/addDoctor")
    @ResponseBody
    public ResultMessage addDoctor(DoctorInfo doctorInfo, String username, String password, int age) {
        ResultMessage resultMessage=null;
        //判断账号是否存在
        Boolean b = userLoginService.finusernameByusername(username);
        if (b) {
            //新增账号
            loginService.addUserDoctor(username, password, age);
            //根据用户名查询id
            String id = loginService.finIdByusername(username);
            doctorInfo.setU_id(id);
            //添加医生数据
            int count = doctorInfoService.addDoctor(doctorInfo);
            if (count > 0) {
                resultMessage = new ResultMessage(200, "添加成功");
            } else {
                resultMessage = new ResultMessage(500, "添加失败");
            }
        }else{
            resultMessage = new ResultMessage(400, "账号已存在！");
        }
        return resultMessage;
    }
    /**
     * 地图页面
     */
    @RequestMapping("/map")
    public String map(){
        return "map";
    }
    /**
     * 删除医生账号
     */
    @RequestMapping("/deleteDoctor")
    @ResponseBody
    public ResultMessage deleteDoctor(String id){
        System.out.println(id+"------------");
        ResultMessage resultMessage=null;
        //删除账户
        doctorInfoService.deleteUser(id);
        //删除医生信息
        int count = doctorInfoService.deleteDoctor(id);
        if (count > 0) {
            resultMessage = new ResultMessage(200, "删除成功");
        } else {
            resultMessage = new ResultMessage(500, "删除失败");
        }
        return  resultMessage;
    }

}
